package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class DishController {

    @Autowired
    private DishService dishService;

    //分页查询
    @GetMapping("/dish/page")                   //设置默认值,更改别名 pageNum
    public ResultInfo findBypage(@RequestParam(name = "page", defaultValue = "1") Integer pageNum,
                                 Integer pageSize, String name) {

        //调用service查询
        Page<Dish> page = dishService.findBypage(pageNum, pageSize, name);
        //返回结果
        return ResultInfo.success(page);
    }

    // 菜品管理=> 新增菜品
    @PostMapping("/dish")
    public ResultInfo save(@RequestBody Dish dish) {
        //调用service保存
        dishService.save(dish);
        //返回成功消息
        return ResultInfo.success();
    }

    // 修改先回显菜品
    @GetMapping("/dish/{id}")
    public ResultInfo findById(@PathVariable Long id) {
        // 调用service查询
        Dish dish = dishService.findById(id);
        //返回结果
        return ResultInfo.success(dish);
    }

    // 菜品管理 => 修改菜品
    @PutMapping("/dish")
    public ResultInfo updateById(@RequestBody Dish dish) {
        //调用service修改保存
        dishService.updateById(dish);
        //返回消息
        return ResultInfo.success();
    }


    //菜品管理 =>删除/批量删除
    @DeleteMapping("/dish")
    public ResultInfo deleteBatchIds(@RequestParam List<Long> ids) {
        //调用service删除
        dishService.deleteBatchIds(ids);
        //返回消息
        return ResultInfo.success();
    }


    //菜品启售 禁售
    @PostMapping("/dish/status/{status}")
    public ResultInfo updateStatus(@PathVariable Integer status, @RequestParam List<Long> ids) {
        //调用service修改菜品状态
        dishService.updateStatus(status, ids);
        //返回信息
        return ResultInfo.success();
    }


    // 根据分类查询菜品列表(回显)
    @GetMapping("/dish/list")
    public ResultInfo findListByCategoryId(Long categoryId ) {
        //调用service
        List<Dish> dishList = dishService.findListByCategoryId(categoryId);
        //返回
        return ResultInfo.success(dishList);
    }

    //根据条件菜品列表(回显)
    @GetMapping(value = "/dish/list",params = {"name"})
    public ResultInfo findByname(String name) {
        //调用service
        List<Dish> dishList = dishService.findByname(name);
        //返回
        return ResultInfo.success(dishList);

    }
}
